﻿using System;
using UnityEngine;
using UnityEngine.Analytics;

public class EditGenderAndBirthUI : MonoBehaviour
{
    public ListTogglesGroup genderGroup;
    public ListTogglesGroup decadeGroup;
    public ListTogglesGroup yearsGroup;

    int m_Gender;
    int m_Decade;
    int m_Year;

    public NextBtn nextBtn;

    public Action<int, int> onEditEnd;

    void OnEnable()
    {
        m_Gender = m_Decade = m_Year = 0;

        genderGroup.Init(OnSelectGender, -1);
        decadeGroup.Init(OnSelectDecade, 90);
        yearsGroup.Init(OnSelectYear, -1);

        nextBtn.onClick = this.OnClickNext;
        nextBtn.SetText("下一步(1/3)");
        nextBtn.Disactive();
    }

    private void OnSelectGender(int gender)
    {
        m_Gender = gender;

        CheckEnd();
    }

    void OnSelectDecade(int decade)
    {
        m_Decade = decade;

        for(int i = 0; i < this.yearsGroup.toggleItems.Count; i++)
        {
            yearsGroup.toggleItems[i].titleText.text = (1900 + decade + i).ToString();
        }

        CheckEnd();
    }

    void OnSelectYear(int year)
    {
        m_Year = year; 

        CheckEnd();
    }

    void CheckEnd()
    {
        if (this.m_Gender > 0 && m_Decade > 0 && m_Year > 0)
        {
            nextBtn.Active();
        }
    }

    public void OnClickNext()
    {
        onEditEnd?.Invoke(m_Gender, 1900 + m_Decade + m_Year);
    }
}
